#include "PkgBufMgr.h"

#define PACKAGE_BUFFER_MAX 2048

CPkgBufMgr::CPkgBufMgr()
{
}

CPkgBufMgr::~CPkgBufMgr()
{
    Destroy();
}

PkgBuf *CPkgBufMgr::GetPkgBuf()
{
    PkgBuf *pbRet = nullptr;
    uint16_t nLen = PACKAGE_BUFFER_MAX - sizeof(PkgBufNode);

    if (m_lstFree.IsEmpty())
        pbRet = m_lstFree.NewNode(nLen);
    else
        pbRet = m_lstFree.PopFront();

    if (nullptr != pbRet)
    {
        pbRet->pBuffer = (uint8_t *)pbRet + sizeof(PkgBufNode);
        pbRet->nMaxLen = nLen;
        pbRet->Reset();
    }

    return pbRet;
}

void CPkgBufMgr::FreePkgBuf(PkgBuf *pb)
{
    if (nullptr != pb)
    {
        m_lstFree.PushBack((PkgBufNode *)pb);
    }
}

void CPkgBufMgr::Destroy()
{
    m_lstFree.Destroy();
}